package cn.beecode.zookeeper.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class LeaderSelect1 {
	private static final String PATH = "/leader";  
    private static final int COUNT = 3;  
  
    public static void main(String[] args) throws Exception  
    {  
        CuratorFramework client = CuratorFrameworkFactory.newClient("10.2.9.59:2181", new ExponentialBackoffRetry(1000, 3));  
        client.start();  
  
        List<LeaderLatch> list = new ArrayList<LeaderLatch>();  
  
        for (int i = 1; i <= COUNT; i++)  
        {  
            LeaderLatch example = new LeaderLatch(client, PATH, "Client" + i);  
            example.start();  
            list.add(example);  
        }  
 
          
        for (LeaderLatch ll : list)  
        {  
            System.out.println(ll.getId() + "\t" + ll.hasLeadership());  
        }  
  
        for (LeaderLatch ll : list)  
        {  
            ll.close();  
        }  
  
        client.close();  
    }  
}
